<template>
	<div class="mainsu">
		<div class="main_spac1">
			<div class="main_white padding10 paddingBottom20">
				<div class="mainor_form">
					<div class="mainor_te">
						<el-button type="primary" @click="exportform">导出</el-button>
					</div>
					<el-form
						:inline="true"
						:model="form"
						class="demo-form-inline"
						ref="userListFrom"
						label-width="70px"
					>
						<el-form-item label="" prop="date_type" class="wid110">
							<el-select v-model="form.date_type" placeholder="请选择">
								<el-option label="采购日期" value="1"> </el-option>
								<el-option label="创建日期" value="2"> </el-option>
							</el-select>
						</el-form-item>
						<el-form-item label="" prop="establishTime">
							<el-date-picker
								v-model="establishTime"
								type="daterange"
								class="time220"
								:editable="false"
								unlink-panels
								range-separator="至"
								start-placeholder="开始日期"
								end-placeholder="结束日期"
								value-format="yyyy-MM-dd"
								:clearable="false"
								:picker-options="pickerOptions"
								@change="dateChange"
							/>
						</el-form-item>
						<el-form-item label="采购方式" prop="mode">
							<el-select
								v-model.trim="form.mode"
								placeholder="采购方式"
								class="wid110"
							>
								<el-option label="所有" value=""></el-option>
								<el-option label="采购员" value="1"></el-option>
								<el-option label="供应商" value="2"></el-option>
							</el-select>
							<el-select
								v-show="form.mode == 1"
								v-model.trim="form.buyer_id"
								placeholder="采购员"
								class="wid220"
							>
								<el-option label="所有" value=""></el-option>
								<el-option
									v-for="(buyer, index) in buyer"
									:key="index"
									:label="buyer"
									:value="parseInt(index)"
								></el-option>
							</el-select>
							<el-select
								v-show="form.mode == 2"
								v-model.trim="form.supplier_id"
								placeholder="供应商"
								class="wid220"
							>
								<el-option label="所有" value=""></el-option>
								<el-option
									v-for="(supplier, index) in supplier"
									:key="index"
									:label="supplier"
									:value="parseInt(index)"
								></el-option>
							</el-select>
							<el-select
								v-show="!form.mode"
								v-model.trim="form_mode_all"
								placeholder="所有"
								class="wid220"
							>
								<el-option label="所有" value=""></el-option>
							</el-select>
						</el-form-item>

						<el-form-item>
							<el-button
								class="reset marginLeft10"
								type="primary"
								@click="reset"
								>重置</el-button
							>
							<el-button
								type="primary"
								@click="getArticleList"
								:loading="listLoading"
								>搜索</el-button
							>
						</el-form-item>
					</el-form>
				</div>
				<div class="main_table marginTop10" style="min-height: 550px">
					<el-table
						ref="multipleTable"
						v-loading="listLoading"
						:data="list"
						element-loading-text="加载中"
						fit
						border
						highlight-current-row
						:header-cell-class-name="this.$verticaline"
					>
						<el-table-column
							label="采购日期"
							prop="day"
							min-width="110"
							show-overflow-tooltip
						>
							<template slot-scope="scope">{{
								parseTime(scope.row.day, "{y}-{m}-{d}")
							}}</template>
						</el-table-column>
						<el-table-column
							label="采购单数量"
							prop="order_num"
							min-width="110"
							show-overflow-tooltip
						>
							<template slot-scope="scope">{{ scope.row.order_num }}</template>
						</el-table-column>

						<el-table-column
							label="采购金额"
							prop="purchase_money"
							show-overflow-tooltip
							min-width="110"
						>
							<template slot="header">
								<headerSortArrow
									label="purchase_money"
									ref="arrow1"
									@handleSort="handleSort($event, 'arrow1')"
									>采购金额</headerSortArrow
								></template
							>
							<template slot-scope="scope">
								{{ scope.row.purchase_money | twoPlace }}
							</template>
						</el-table-column>
						<el-table-column
							label="退货单数量"
							prop="return_order_num"
							min-width="110"
							show-overflow-tooltip
						>
						</el-table-column>

						<el-table-column
							label="退货金额"
							prop="return_money"
							show-overflow-tooltip
							min-width="110"
						>
							<template slot="header">
								<headerSortArrow
									label="return_money"
									ref="arrow2"
									@handleSort="handleSort($event, 'arrow2')"
									>退货金额</headerSortArrow
								></template
							>
							<template slot-scope="scope">
								{{ scope.row.return_money | twoPlace }}
							</template>
						</el-table-column>
						<el-table-column
							label="实采购金额"
							prop="real_money"
							min-width="110"
							show-overflow-tooltip
						>
							<template slot="header">
								<headerSortArrow
									label="real_money"
									ref="arrow3"
									@handleSort="handleSort($event, 'arrow3')"
									>实采购金额</headerSortArrow
								></template
							>
							<template slot-scope="scope">
								{{ scope.row.real_money | twoPlace }}
							</template>
						</el-table-column>
						<el-table-column
							label="管理操作"
							width="120"
							align="center"
							prop=""
							fixed="right"
						>
							<template slot-scope="scope">
								<el-link type="primary" @click="handleDetail(scope.row)"
									>明细</el-link
								>
							</template>
						</el-table-column>
					</el-table>
				</div>
				<div class="main_page boxdq">
					<div></div>
					<!-- 分页 -->
					<div class="main_page_r">
						<el-pagination
							:current-page="current.page"
							:page-sizes="[20, 30, 50, 100]"
							:page-size="limit"
							layout="total, sizes, prev, pager, next, jumper"
							:total="totalCount"
							background
							@size-change="changeLimit"
							@current-change="changePage"
						/>
					</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
	import {
		get_zcGetPurchaseReportList,
		export_purcahse_order,
		get_report_base,
	} from "@/api/report/index";
	import { apiUrl, objKeySort, Md5 } from "@/utils/public";
	import { report_shortcuts } from "@/options/new_dateOption";
	import { parseTime, exportMethod, getNowMonth } from "@/utils/index";
	import headerSortArrow from "@/views/report/components/headerSortArrow";
	export default {
		components: { headerSortArrow },
		data() {
			return {
				pickerOptions: report_shortcuts,
				exportMethod,
				getNowMonth,
				// 时间
				parseTime,
				// 分页
				current: {
					page: 1,
					per_page: 20,
				},
				limit: 20,
				totalCount: 1,
				// 列表数据
				list: [],
				listLoading: false,
				// 搜索
				form: {
					start_time: "",
					end_time: "",
					mode: "",
					supplier_id: "",
					buyer_id: "",
					date_type: "1",
					order_by: "",
					order_type: "", //1升序  2降序
				},
				buyer: {},
				supplier: {},
				form_mode_all: "",
				establishTime: [],
			};
		},
		watch: {
			"form.mode": {
				handler(val) {
					if (val == 1) {
						this.form.supplier_id = null;
					} else if (val == 2) {
						this.form.buyer_id = null;
					} else if (!val) {
						this.form.supplier_id = null;
						this.form.buyer_id = null;
					}
				},
				deep: true,
			},
		},
		created() {
			this.establishTime = this.getNowMonth();
			this.get_report_base();
			// 列表
			this.get_zcGetPurchaseReportList();
		},
		methods: {
			// 基本信息
			get_report_base() {
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name
				);
				get_report_base(data).then((res) => {
					if (res.errcode == 0) {
						this.supplier = res.data.supplier;
						this.buyer = res.data.buyer;
					}
				});
			},
			get_zcGetPurchaseReportList() {
				this.listLoading = true;
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					this.list_form(),
					this.current
				);
				get_zcGetPurchaseReportList(data).then((res) => {
					if (res.errcode == 0) {
						this.list = res.data.list;
						this.totalCount = res.data.count;
					} else {
						this.$message.error(res.errmsg);
					}
					this.listLoading = false;
				});
			},
			// 重置
			reset() {
				this.$refs["userListFrom"].resetFields();
				this.establishTime = this.getNowMonth();
				this.form.supplier_id = null;
				this.form.buyer_id = null;
				this.getArticleList();
			},
			// 搜索
			getArticleList() {
				this.current.page = 1;
				this.get_zcGetPurchaseReportList();
			},
			// 分页
			changeLimit(pageSize) {
				this.current.per_page = pageSize;
				this.get_zcGetPurchaseReportList();
			},
			changePage(page) {
				this.current.page = page;
				this.get_zcGetPurchaseReportList();
			},

			list_form() {
				let form = JSON.parse(JSON.stringify(this.form));
				form.start_time = this.establishTime[0] ? this.establishTime[0] : "";
				form.end_time = this.establishTime[1] ? this.establishTime[1] : "";
				if (!form.mode) {
					delete form.mode;
				}
				return form;
			},
			// 排序
			handleSort(data, ref) {
				let arr = ["arrow1", "arrow2", "arrow3"].filter((e) => e != ref);
				arr.map((e) => {
					this.$refs[e].selected = false;
				});
				this.form.order_by = data.label;
				this.form.order_type = data.type;
				this.get_zcGetPurchaseReportList();
			},
			// 导出
			exportform() {
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					this.list_form()
				);
				export_purcahse_order(data).then((res) => {
					this.exportMethod(res, "采购报表-订单");
				});
			},
			dateChange(val) {
				let start = new Date(val[0]).getTime();
				let end = new Date(val[1]).getTime();
				let diff = start - end;
				if (Math.abs(diff) > 3600 * 1000 * 24 * 60) {
					this.$message.warning("选择时间跨度不能超过两个月");

					this.establishTime = this.getNowMonth();
				}
				this.get_zcGetPurchaseReportList();
			},
			// 明细
			handleDetail(row) {
				let day = row.day;
				this.$router.push({
					name: "purchasing_purchase",
					params: {
						day: day,
						mode: this.form.mode,
						supplier_id: this.form.supplier_id,
						buyer_id: this.form.buyer_id,
					},
				});
			},
		},
	};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
	.mainor_form form {
		width: calc(100% - 180px);
	}
}

// 支付状态
.success {
	color: $green;
}
.unpaid {
	color: $colour_red;
}
// 订单状态
.order0 {
	color: $order_color1;
}
.order1 {
	color: $order_color2;
}
.order2 {
	color: $order_color3;
}
.order4 {
	color: $order_color4;
}
</style>
